package com.guxingke.typecho4j.widget.handle.user;

import javax.servlet.http.HttpSession;

import com.guxingke.typecho4j.Typecho4jException;
import com.guxingke.typecho4j.jfinal.model.User;
import com.guxingke.typecho4j.route.DispatcherStrategy;
import com.guxingke.typecho4j.route.Route;
import com.guxingke.typecho4j.route.Routes;
import com.guxingke.typecho4j.widget.WidgetHandle;
import com.guxingke.typecho4j.widget.provider.UserBaseWidget;
import com.jfinal.core.Controller;

/**
 * 更新个人设置的处理组件
 * 
 * @author guxingke
 *
 */
public class UpdateProfileHandleWidget extends UserBaseWidget implements
		WidgetHandle {

	@Override
	public void handle(Controller controller) throws Typecho4jException {
		User user = controller.getModel(User.class);
		user.update();
		user = User.dao.findById(user.get("uid"));
		updateSession(controller.getSession(), user);
	}

	private void updateSession(HttpSession session, User user) {
		session.setAttribute("user", user);
	}

	@Override
	public void configRoute(Routes routes) {
		Route updateProfileRoute = new Route("updateProfile",
				this.getClass(), "/admin/profile",
				DispatcherStrategy.REDIRECT);
		routes.addRoute(updateProfileRoute);
	}
}
